package cn.orange.ch03_linked;

import cn.orange.ListNode;

/**
 * LC203.移除链表元素
 给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点
 */
public class LC203 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(-1, head);
        ListNode prev = dummy;
        while (prev.next != null) {
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }
        return dummy.next;
    }
    public static void main(String[] args) {
        LC203 alg = new LC203();
        ListNode h1 = new ListNode(1, new ListNode(2, new ListNode(6, new ListNode(4, new ListNode(5, new ListNode(6))))));
        System.out.println(alg.removeElements(h1, 6));
        System.out.println(alg.removeElements(null, 1));
        ListNode h2 = new ListNode(7, new ListNode(7, new ListNode(7, new ListNode(7, new ListNode(7, new ListNode(7))))));
        System.out.println(alg.removeElements(h2, 7));
    }
}
